﻿using System;
using System.Data;
using System.Linq;
using System.Windows;
using WPFExcelReport.Chart;
using System.Windows.Controls;
using WPFExcelReport.Helpers;
using System.ComponentModel;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for EditEventWindow.xaml
    /// </summary>
    public partial class TVKPVAPR
    {
        public TVKPVAPR()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VVKPOS();
            this.DataContext = MV;
            InitializeComponent();
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            if (dwMain == null)
            {
            }
            if (dwMaster == null)
            {
                KEY = "";
                VART = "";
            }
            else
            {
                KEY = TMV.VKPAUFKRS + TMV.VKPAUFLFD + TMV.VKPAUFPOS + TMV.VKPKUNDNR + "K";
                VART = TMV.VKPKOLL + TMV.VKPART + TMV.VKPFARBE;
            }
        }
        public override void LOADBYAUF()
        {
            TBMain.Clear();
            base.LOADBYAUF();
            if (ORDERNUM.Length < 13 + 29)
                return;
            KRS = ORDERNUM.Substring(0, 2);
            LFD = ORDERNUM.Substring(2, 6);
            KOLL = ORDERNUM.Substring(13, 5);
            ART = ORDERNUM.Substring(18, 24);
            dwData = TBMain.NewRow();
            dwData["VKPMANDANT"] = app.Mandant;
            dwData["VKPAUFKRS"] = KRS;
            dwData["VKPAUFLFD"] = LFD;
            dwData["VKPKOLL"] = KOLL;
            dwData["VKPART"] = ART;
            dwData["VKPSA"] = VKPSA;
            SResult = TMV.QueryVKPOS(dwData, 19);
            if (SResult.Tables[0].Rows.Count == 0)
            {
                KRS = ORDERNUM.Substring(0, 2);
                LFD = ORDERNUM.Substring(2, 6);
                KOLL = ORDERNUM.Substring(13, 5);
                ART = ORDERNUM.Substring(18, 24);
                POSTYP = "OF ";
                New();
            }
            else
            {
                MergeData(SResult, true);
            }
        }

        public override void OnAUFChg(string auf)
        {
            TBMain.Clear();
            if (auf.Length < 11)
                return;
            KRS = auf.Substring(1, 2);
            LFD = auf.Substring(3, 6);
            POS = auf.Substring(9, 3);
            dwData = TBMain.NewRow();
            dwData["VKPMANDANT"] = app.Mandant;
            dwData["VKPAUFKRS"] = KRS;
            dwData["VKPAUFLFD"] = LFD;
            dwData["VKPAUFPOS"] = POS; 
            SResult = TMV.QueryVKPOS(dwData, 1); 
            MergeData(SResult, true); 
        }

        public override void OnSave()
        {
            if (!IsSaved)
                return;
            if (dwMaster == null)
                return;
            UPDATEVKP(dwMaster);
        }
    }
}
